<template>
  <div style="padding: 10px;">
    <!-- 搜索区域 -->
    <div style="margin: 10px 0;">
      <el-input v-model="params.title" placeholder="请输入书籍名称" style="width: 20%;" @keyup.enter="load"></el-input>
      <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh"></i>重置</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="bookId" label="书籍序号" style="width: 100px;" />
      <el-table-column prop="title" label="书籍名称" />
      <el-table-column prop="author" label="作者" />
      <el-table-column prop="publisher" label="出版社" />
      <el-table-column prop="publishDate" label="出版日期">
        <template #default="scope">
          {{ formatDate(scope.row.publishDate) }}
        </template>
      </el-table-column>
      <el-table-column prop="category" label="分类" />
      <el-table-column prop="borrowDate" label="借阅日期">
        <template #default="scope">
          {{ formatBorrowDate(scope.row.borrowDate) }}
        </template>
      </el-table-column>
      <el-table-column prop="loginId" label="用户ID" />
      <el-table-column prop="username" label="用户昵称" />
      <el-table-column fixed="right" label="操作" min-width="120">
        <template #default="scope">
          <el-button :disabled="scope.row.status !== 0" @click="handleReturn(scope.row)">归还</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div style="margin: 10px 0;">
      <el-pagination
          v-model:current-page="params.pageNum"
          v-model:page-size="params.pageSize"
          :disabled="disabled"
          :background="background"
          layout="prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import request from '@/utils/request';
import moment from 'moment';

export default {
  name: 'BorrowView',
  data() {
    return {
      search: '',
      params: {
        title: '',
        pageSize: 10,
        pageNum: 1
      },
      total: 0,
      tableData: []
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      request.get('/borrow/page', {
        params: this.params
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.list;
          this.total = res.data.total;
        }
      });
    },
    handleReturn(row) {
      request.put(`/borrow/return/${row.recordId}`)
          .then(res => {
            if (res.code === '200') {
              this.$message.success('书籍已归还');
              // 调用新的接口来更新totalCopies
              request.put(`/book/incrementTotalCopies`, null, { params: { title: row.title } })
                  .then(res => {
                    if (res.code === '200') {
                      this.$message.success('书籍库存已更新');
                      this.load(); // 重新加载表格数据
                    } else {
                      this.$message.error('更新库存失败');
                    }
                  })
                  .catch(error => {
                    console.error('Error updating book totalCopies:', error);
                    this.$message.error('更新库存失败');
                  });
            } else {
              this.$message.error('归还失败');
            }
          })
          .catch(error => {
            console.error('Error returning book:', error);
            this.$message.error('归还失败');
          });
    },
    handleSizeChange(size) {
      this.params.pageSize = size;
      this.load();
    },
    handleCurrentChange(page) {
      this.params.pageNum = page;
      this.load();
    },
    reset() {
      this.params = {
        title: '',
        pageSize: 10,
        pageNum: 1
      };
      this.load();
    },
    formatDate(date) {
      return moment(date).format('YYYY-MM');
    },
    formatBorrowDate(date) {
      return moment(date).format('YYYY-MM-DD');
    }
  }
};
</script>

<style scoped>
/* 样式 */
</style>